<template>
    <div>
        <commpVxeTable
         :config="dataTableConfig" 
         :columns="columnsVxe" 
         :formItem="formItem"
         @checkboxChange="checkboxChange"
         ref="questionnaireManagementRef"
         @searchEvent="searchEvent"
         @resetEvent="resetEvent"
         >
            <template v-slot:externalOperations="{row}">
                <vxe-form-item>
                    <template>
                        <a-button :loading="packExportLoading" class="blue-btn" type="primary" @click="packExportClick()">打包导出问卷</a-button>
                    </template>
                </vxe-form-item>
            </template>
            <template v-slot:ftxrq_time>
                <a-date-picker v-model:value="formItemData.ftxrq" />
            </template>
            <template v-slot:ftjrq_time>
                <a-date-picker v-model:value="formItemData.ftjrq" />
            </template>
            <template v-slot:operation_btn="{row}" data-desc="自定操作栏">
                <a-form layout="inline" class="search-form" >
                    <a-form-item >
                        <a-button class="green-btn" type="primary" @click="viewClick(row.id)">查看问卷</a-button>
                    </a-form-item>
                    <a-form-item >
                        <a-button :loading="exportQuestionnaireLoading" class="blue-btn" type="primary" @click="exportQuestionnaireClick(row)">导出问卷</a-button>
                    </a-form-item>
                    <a-form-item >
                        <a-button class="red-btn" type="primary" @click="deleteClick(row.id)">删除</a-button>
                    </a-form-item>
                </a-form>
            </template>
        </commpVxeTable>
        <questionnaire :questionnaireId="questionnaireId" ref="viewRef"/>
    </div>
</template>
<script>
import {
    wjjbxxPageList,
    initWjjbxx,
    selectDjDa,
    submitWj,
    deleteBatchWjjbxx,
    spcyWjWjjbxxexportWjFilePdf,
    spcyWjWjjbxxexportWjFileZip,
} from './service/index'
import assessCheckApi from './service/api.js'
import commpVxeTable from '@/components/commp-vxe-table/commp-vxe-table.vue'
import questionnaire from './questionnaire.vue'
import moment from 'moment'
import blobType from '@/common/blobType'
export default {
    components:{
        commpVxeTable,
        questionnaire
    },
    props:{
        fwjlx:{
            type:String,
            default: () => '',
            required:false
        },
    },
    data(){
        return{
            formItem:[
                {
                    label: '问卷标题',
                    model: 'fwjbt',
                },{
                    label: '提交状态',
                    model: 'flag',
                    options:[{value:'是',label:'是'},{value:'否',label:'否'}],
                    type:'Select'
                },{
                    label: '受访单位',
                    model: 'fsfdwmc',
                },{
                    label: '受访地址',
                    model: 'fsfdwdz',
                },{
                    label: '联系人',
                    model: 'flxr',
                },{
                    label: '联系电话',
                    model: 'flxdh',
                },{
                    label: '填写日期',
                    model: 'ftxrq',
                    slotName:'ftxrq_time'
                },{
                    label: '提交日期',
                    model: 'ftjrq',
                    slotName:'ftjrq_time'
                }
            ],
            dataTableConfig:{
                api: assessCheckApi.wjjbxxPageList,
                rowId: 'id',
                paramet:{
                    fwjlx:this.fwjlx
                }
            },
            columnsVxe:[
                { 
                    type: 'checkbox',
                    width: 60 ,
                    fixed: 'left'
                },{
                    title: '问卷标题', //列头显示文字
                    field: 'fwjbt',
                },{
                    title: '受访单位', //列头显示文字
                    field: 'fsfdwmc',
                },{
                    title: '受访地址', //列头显示文字
                    field: 'fsfdwdz',
                },{
                    title: '联系人', //列头显示文字
                    field: 'flxr',
                },{
                    title: '联系电话', //列头显示文字
                    field: 'flxdh',
                },{
                    title: '填写日期', //列头显示文字
                    field: 'ftxrq',
                },{
                    title: '提交日期', //列头显示文字
                    field: 'ftjrq',
                },{
                    title: '操作',
                    field: 'id',
                    slots: {
                        // 使用插槽模板渲染
                        default: 'operation_btn',
                    },
                    fixed: 'right',
                    width:255,
            }],
            selectedRowKeysArr:[],
            visibleQRQrCode:false,
            formItemData:{
                ftxrq:'',
                ftjrq:'',
            },
            questionnaireId:'',
            packExportLoading:false,
            exportQuestionnaireLoading:false,
        }
    },
    watch:{

    },
    mounted(){
        
        
    },
    methods:{
        searchEvent(){//搜索
            this.dataTableConfig.paramet = {
                ...this.dataTableConfig.paramet,
                ftxrq:this.formItemData.ftxrq?moment(this.formItemData.ftxrq).format('YYYY-MM-DD') + ' 00:00:00':'',
                ftjrq:this.formItemData.ftjrq?moment(this.formItemData.ftjrq).format('YYYY-MM-DD') + ' 00:00:00':'',
            }
            //console.log(this.dataTableConfig.paramet)
        },
        resetEvent(){//重置
            this.dataTableConfig.paramet = {
                ...this.dataTableConfig.paramet,
            }
            this.formItemData.ftxrq = ''
            this.formItemData.ftjrq = ''

        },
        checkboxChange(val){//获取当前选择的行
            //console.log('当前选择表格',val)
            this.selectedRowKeysArr = []
            val.forEach(item => {
                this.selectedRowKeysArr.push(item.id)
            });
        },
        deleteClick(val){
            var that = this
            this.$confirm({
                title:'您确认要删除吗？',
                // icon: createVNode(ExclamationCircleOutlined),
                onOk() {
                    deleteBatchWjjbxx({ids:val}).then(res=>{
                        if(res.code == 200){
                            that.$message.success(res.message)
                            that.$refs.questionnaireManagementRef.searchEvent()
                        }else{
                            that.$message.warning(res.message)
                        }
                    })
                },
                
            });
        },
        viewClick(val){
            this.$refs.viewRef.visibleShow = true
            this.questionnaireId = val
        },
        packExportClick(){
            if(!this.selectedRowKeysArr.length){
                this.$message.warning('请先选择打包导出项！')
                return
            }
            
            this.exportQuestionnaireLoading = true
            spcyWjWjjbxxexportWjFileZip({ids:this.selectedRowKeysArr.join(',')}).then((data) => {
                if (!data) {
                    this.$message.warning('文件下载失败')
                    return
                }
                    let url = window.URL.createObjectURL(new Blob([data], { type: blobType.type[blobType.suffix.indexOf('.zip')] }))
                    let link = document.createElement('a')
                    link.style.display = 'none'
                    link.href = url
                    link.setAttribute('download', '满意度调查表压缩包.zip' )
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link) //下载完成移除元素
                    window.URL.revokeObjectURL(url) //释放掉blob对象
                    this.$refs.questionnaireManagementRef.searchEvent()
                    this.exportQuestionnaireLoading = false
            })
        },
        exportQuestionnaireClick(row){
            this.packExportLoading = true
            spcyWjWjjbxxexportWjFilePdf({id:row.id}).then((data) => {
                if (!data) {
                    this.$message.warning('文件下载失败')
                    return
                }
                    let url = window.URL.createObjectURL(new Blob([data], { type: blobType.type[blobType.suffix.indexOf('.pdf')] }))
                    let link = document.createElement('a')
                    link.style.display = 'none'
                    link.href = url
                    link.setAttribute('download', '满意度调查表.pdf' )
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link) //下载完成移除元素
                    window.URL.revokeObjectURL(url) //释放掉blob对象
                    this.packExportLoading = false
            })
        },
    }
}
</script>
<style>
.red-btn{
    border: 1px solid #ed5565 !important;
    background-color: #ed5565 !important;
}
</style>